import 'package:flutter/material.dart';

class ListViewScreenState extends State<ListViewScreen> {
  @override
  Widget build(BuildContext context) {
    return ListView(
      //调用默认无名构造函数，传入要展示的widget即可
      children: <Widget>[
        Text("Item1"),
        Text("Item2"),
        Text("Item3"),
        Text("Item4"),
        Text("Item5")
      ],
    );
  }
}

class ListViewScreenState2 extends State<ListViewScreen> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        scrollDirection: Axis.vertical,
        //滚动方向
        reverse: false,
        // 是否颠倒展示 默认false，true效果 相当于将手机上下颠倒 好理解一点
        itemCount: 50,
        //列表总条数
        //controller: null,//滚动控制器
        primary: true,
        physics: AlwaysScrollableScrollPhysics(),
        //滚动交互效果 BouncingScrollPhysics  ClampingScrollPhysics AlwaysScrollableScrollPhysics NeverScrollableScrollPhysics
        shrinkWrap: true,
        padding: EdgeInsets.all(8),
        //内间距
        //itemExtent: 80,//条目大小 传则固定指定大小（高度/宽度）不传则只适应高度/宽度
        cacheExtent: 1,
        //缓存大小

        itemBuilder: (BuildContext context, int index) {
          if (index == 2)
            return Text(
                "Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text ");
          return Text("item: $index");
        });
  }
}

class ListViewScreenState3 extends State<ListViewScreen> {
  @override
  Widget build(BuildContext context) {
    return ListView.separated(
        itemCount: 10,
        separatorBuilder: (BuildContext context, int index) {
          //奇数
          return Text("----->--- $index"); //分割视图
        },
        itemBuilder: (BuildContext context, int index) {
          //偶数
          if (index == 2)
            return Text(
                "Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text Long Text ");
          return Text("item: $index");
        });
  }
}

class ListViewScreenState4 extends State<ListViewScreen> {
  @override
  Widget build(BuildContext context) {
    return ListView.custom(
      childrenDelegate:
          SliverChildBuilderDelegate((BuildContext context, int index) {
        return Text("xxx: $index");
      }),
    );
  }
}

class MyScrollController extends ScrollController {
  @override
  ScrollPosition createScrollPosition(ScrollPhysics physics,
      ScrollContext context, ScrollPosition oldPosition) {
    return super.createScrollPosition(physics, context, oldPosition);
  }
}

class ListViewScreenState5 extends State<ListViewScreen> {
  ScrollController _controller = ScrollController(
      initialScrollOffset: 100, keepScrollOffset: false, debugLabel: "adfad");
  var _scrollListener = () {
    print("scrolling....");
  };

  @override
  void initState() {
    _controller.addListener(_scrollListener);
    super.initState();
  }

  @override
  void dispose() {
    _controller.removeListener(_scrollListener);
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scrollbar(
        child: ListView.custom(
      controller: _controller, //滚动控制器
      childrenDelegate:
          SliverChildBuilderDelegate((BuildContext context, int index) {
        return Container(
          child: Text("item $index"),
          color: Color.fromRGBO(10, 10, 10, 0.5),
        );
      }, childCount: 50),
    ));
  }
}

void main() {
  runApp(new MaterialApp(
      title: "App", theme: new ThemeData.light(), home: ListViewScreen()));
}

class ListViewScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ListViewScreenState5();
  }
}
